import http from '../../apis/api.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    custNo:'',
    storeNo:'',
    storeName:'',
    level:5,
    submitForm:{
      storeScore:0,
      target1:0,
      target2:0,
      target3:0,
      depict:''
    },
    error:{
      storeScore:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   self = this;
    self.data.custNo = options.custNo;
    self.data.storeNo = options.storeNo;
    wx.setNavigationBarTitle({
      title: decodeURIComponent(options.storeName)
    })
    self.setData({
      storeName: self.data.storeName
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '一券在手游天下',
      imageUrl: '/images/indexShare.jpg',
      path:'/pages/index/index'
    }
  },
  clickLevel(e){
    let type=e.currentTarget.dataset.type;
    let index = e.currentTarget.dataset.index;
    switch (type){
      case "storescore":
        self.data.submitForm.storeScore=index;
      break;
      case "target1":
        self.data.submitForm.target1 = index;
        break;
      case "target2":
        self.data.submitForm.target2 = index;
        break;
      case "target3":
        self.data.submitForm.target3 = index;
        break;
    }
    self.setData({
      submitForm: self.data.submitForm
    })
  },
  blurArea(e){
    self.data.submitForm.depict=e.detail.value;
    self.setData({
      submitForm:self.data.submitForm
    })
  },
  checked(name) {
    switch (name) {
      case 'storeScore':
        self.data.error.storeScore = self.data.submitForm.storeScore>0? '' : '选择总体评价';
        return self.data.submitForm.storeScore >0 ? true : false;
        break;
    }
  },
  submitEval(e){
    if (self.checked('storeScore')){
      wx.showLoading()
      self.setData({
        error: self.data.error
      })
      let data= {
        custNo: self.data.custNo,
        storeNo: self.data.storeNo,
        storeScore: Number(self.data.submitForm.storeScore),
        target1: Number(self.data.submitForm.target1),
        target2: Number( self.data.submitForm.target2),
        target3: Number(self.data.submitForm.target3),
        depict: self.data.submitForm.depict,
      }
      http.saveEvaluate(data).then(res=>{
        wx.hideLoading();
        if (res.code==0){
          wx.showToast({
            title: '评价成功',
            icon: 'success',
            duration: 2000,
            complete(){
              wx.redirectTo({
                url: `/pages/evaluateList/evaluateList?storeNo=${self.data.storeNo}`
              })
            }
          })
        }
      })
    }else{
      self.setData({
        error:self.data.error
      })
    }
  }
})